/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.game.stepbystep;

import com.nokia.mid.ui.gestures.GestureEvent;
import com.nokia.mid.ui.gestures.GestureInteractiveZone;
import com.nokia.mid.ui.gestures.GestureListener;
import com.nokia.mid.ui.gestures.GestureRegistrationManager;

/**
 *
 * @author zinin
 */
public class MyGesture implements GestureListener{

    private GameLayerManager layerManager;
    private final MIDPCanvas canvas;
    
    MyGesture(MIDPCanvas aCanvas){
        canvas = aCanvas;
        layerManager = canvas.getLayerManager();
        
        // Set the listener to register events for GestureCanvas (this)
        GestureRegistrationManager.setListener(canvas, this);
        GestureInteractiveZone myGestureZone = new GestureInteractiveZone(GestureInteractiveZone.GESTURE_TAP |
                                                                            GestureInteractiveZone.GESTURE_DRAG | 
                                                                            GestureInteractiveZone.GESTURE_DROP);
        // Set the location (relative to the container) and size of the interactive zone:
        myGestureZone.setRectangle(0, 0, canvas.DISP_WIDTH, canvas.DISP_HEIGHT);

        // Register the interactive zone for GestureCanvas (this)
        GestureRegistrationManager.register(canvas, myGestureZone);
    }
    
    public void gestureAction(Object container,
            GestureInteractiveZone gestureInteractiveZone,
            GestureEvent gestureEvent) {
        int x;
        int y;
        try {
            switch (gestureEvent.getType()) {
                case GestureInteractiveZone.GESTURE_TAP:
                    x = gestureEvent.getStartX();
                    y = gestureEvent.getStartY();
                    layerManager.onTapScreen(x, y);
                    break;
                case GestureInteractiveZone.GESTURE_DRAG:
                case GestureInteractiveZone.GESTURE_DROP:
                    x = gestureEvent.getDragDistanceX();
                    y = gestureEvent.getDragDistanceY();
                    layerManager.dragMapPosition(x, y, 
                            (gestureEvent.getType() == GestureInteractiveZone.GESTURE_DROP));
                    break;
            }
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }
    }
}
